package com.qfedu.controller;

import com.qfedu.common.JsonResult;
import com.qfedu.entity.User;
import com.qfedu.service.UserService;
import com.qfedu.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;

/**
 * @Author 刘浩
 * @Date 2020/8/24 21:48
 */
@Controller
@ResponseBody
public class UploadController {
    @Autowired
    private UserService userService;

    @RequestMapping("/upload.do")
    public JsonResult uploadFile(@RequestParam MultipartFile upfile, HttpSession session) {
        User u = (User) session.getAttribute(StrUtils.LOGIN_USER);
//        //要把上传的文件存起来
//        //1.要把图片的路径存起来 存到数据中
//        //2.将上传的图片 放到咱们配置好的虚拟环境下面
        if (u==null){
            return new JsonResult(0,"未登录");
        }
        //1.上传文件的目录
        String dir="E:/upload";
        //2.获取上传文件的名字
        String fileName = upfile.getOriginalFilename();
        //3.判断目录是否存在
        File dirFile = new File(dir);
        if (!dirFile.exists()){
            dirFile.mkdirs();
        }
        //4.要保存文件的file对象
        File newFile = new File(dir, fileName);
        try {
            //保存
            upfile.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //并且写入数据库，要把图片的路径的存起来  存到数据库中
        userService.updateHeadImg(u.getUid(), "/upload/"+ fileName);
        return new JsonResult(1, "上传成功");
    }
}
